package com.gomtel.smartdevice.net;

import android.util.Log;

import com.gomtel.smartdevice.moudle.event.CommonEvent;
import com.gomtel.smartdevice.moudle.event.EventCode;
import com.gomtel.smartdevice.net.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

@SuppressWarnings("ALL")
public class NetUtils {
    private static final String TAG = NetUtils.class.getSimpleName();
    private static final String URL = "http://www.bestfordy.com/Pulsehealth/api/getShowMai";
    //private static final String URL = "http://121.201.80.250/k9_hel/api/getMaiAllParse";
    private static final OkHttpClient okHttpClient = new OkHttpClient();
    private static final String APPID = "oaaE241P70F0EbGKUnowkPp1Q6IE";

    public static void getBody(String high, String low) {
        OkHttpUtils.mThreadPools.execute(() -> {
            //构建一个请求对象
            Request request = new Request.Builder().url(URL +
                    "?high=" +
                    high +
                    "&low=" +
                    low +
                    "&appid=" +
                    APPID).build();
            //发送请求
            try {
                Response response = okHttpClient.newCall(request).execute();
                //打印服务端传回的数据
                if (response.code() == 200 && response.body() != null) {
                    String string = response.body().string();
                    //string = new String(string.getBytes("GBK"), "UTF-8");
                    if (string != null) {
                        EventBus.getDefault().post(new CommonEvent(EventCode.PULSE_TAKING, string));
                        Log.i(TAG, string);
                    }
                } else if (response.code() == 500) {
                    Log.e(TAG, "appid不正确");
                } else if (response.code() == 5001) {
                    Log.e(TAG, "血压参数为空");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    public static class EventClassAndBody {
        public String high;
        public String low;
        public String body;

        public EventClassAndBody() {
        }

        public EventClassAndBody(String high, String low, String body) {
            this.high = high;
            this.low = low;
            this.body = body;
        }

        @Override
        public String toString() {
            return "EventClassAndBody{" +
                    "high='" + high + '\'' +
                    ", low='" + low + '\'' +
                    ", body='" + body + '\'' +
                    '}';
        }
    }
}
